package com.eclipse.e20200202_数据库连接池工具包.com.qf.app1.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.eclipse.e20200202_数据库连接池工具包.com.qf.app1.student.dao.IStudentDao;
import com.eclipse.e20200202_数据库连接池工具包.com.qf.app1.student.dao.StudentDaoImpl;

/**
 * StudentServlet
 */
@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
	private static final Logger logger = LogManager.getLogger();

	// 与数据交互的对象
	private IStudentDao studentDao = new StudentDaoImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 设置响应头：内容类型 ContentType：application/json;charset=utf-8
		response.setContentType("application/json;charset=utf-8");
		// String name = request.getParameter("name");

		List<Student> students = studentDao.search();

		students.forEach((t) -> logger.debug(t));

		//获取输出流
		PrintWriter out = response.getWriter();

		//序列化数据
		ObjectMapper objectMapper = new ObjectMapper();
		String result = objectMapper.writeValueAsString(students);
		logger.debug(result);

		out.print(result);

	}
}
